<!-- 赛事列表 -->
<template>
	<view class="item-block">
		<view class="header-search">
			<u--input placeholder="请输入赛事名称" suffixIcon="search" @confirm='inputconfirm' suffixIconStyle="color: #bfbfbf"
				:focus='focus'></u--input>
		</view>
		<view class="bot-item">
			<view @click="openselect" class="select_item">
				<u--input border="bottom" :placeholder="show?'请选择比赛状态▲':'请选择比赛状态▼'" :disabled='true'
					disabledColor='#f4f4f4' v-model="statename"></u--input>
			</view>
			<view class="item-list">
				<view class="list_item">
					<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Y" @scrolltolower="lower"
						:style="{ height: `calc(100vh - 133px)` }">
						<view class="polular_list_item" v-for="(item,index) in datalist" :key="index"
							@click="itemdetail(item)">
							<view class="but_img" :style="{ backgroundImage: 'url(' + item.urlimg + ')' }">
							</view>
							<view class="item_left">
								<view class="title_item">
									{{ item.raceName.length > 7 ? item.raceName.slice(0, 7) + '...' : item.raceName }}
								</view>
								<view class="team_item">
									参赛队伍：{{item.joinNum}}/{{item.teamNum}}
								</view>
								<view class="address_item">
									<span class="iconfont icon-dizhi"></span>
									<span class="address_text">{{item.placeName}}</span>
								</view>
							</view>
							<view class="item_right">
								<view class="right_top">
									<view class="time">
										{{item.statetype==4?'报名结束时间':(item.statetype==5?'报名开始时间':'赛事开始时间')}}
									</view>
									<view class="time">
										{{item.statetype==4?item.signEnd:(item.statetype==5?item.signStart:item.startTime)}}
									</view>
								</view>
								<view class="time_but">
									<u-button type="primary" class="infobut" text="未开始" color='#828282'
										v-if="item.statetype==5"></u-button>
									<u-button type="primary" v-else color='#de3600'
										:text="item.statetype==1?'已结束':(item.statetype==2?'进行中':(item.statetype==3?'已报满':(item.statetype==4?'报名中':'')))"></u-button>
								</view>
							</view>
						</view>
						<u-loadmore line :status="status" :loading-text="loadingText" :nomore-text="nomoreText"
							v-if="this.pagetotal!=0" />
					</scroll-view>
				</view>
			</view>
		</view>
		<u-toast ref="uToast"></u-toast>
		<!-- ▼▲ -->
		<u-picker :show="show" :columns="columns" title="请选择比赛状态" @cancel="cancel" @confirm="confirm"></u-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				focus: false,
				show: false,
				columns: [
					['全部', '未结束', '已结束']
				],
				datalist: [],
				query: {
					pagNum: 1,
					pageSize: 10,
					raceState: '',
					raceName: '',
				},
				status: 'loading',
				loadingText: '努力加载中',
				nomoreText: '没有更多了',
				statename: '比赛状态 : 全部',
				pagetotal: 0,
			}
		},
		created() {

		},
		onLoad(options) {
			// 从路由参数中获取传递的值			
			const id = decodeURIComponent(options.id);
			if (id == 1) {
				this.focus = true
			}
		},
		mounted() {
			this.eventlist()


		},
		methods: {
			eventlist() {
				const that = this
				this.$request(`/index/raceList`, 'post', this.query)
					.then(
						res => {
							const array = res.data.data.records
							array.forEach(item => {
								let currentTime = new Date();
								// 判断 racestate 是否为 1
								if (item.raceState == 1) {
									item.statetype = 1;
									item.urlimg = 'https://web.ptgolf.net/image/webchat/5.png';
								}
								// 如果 racestate 不是 1，判断当前时间是否大于 startime
								else if (currentTime > new Date(item.startTime)) {
									item.statetype = 2;
									item.urlimg = 'https://web.ptgolf.net/image/webchat/6.png';
								}
								// 如果还是不满足，判断 joinNum 是否等于 teamNum
								else if (item.joinNum == item.teamNum) {
									item.statetype = 3;
									item.urlimg = 'https://web.ptgolf.net/image/webchat/18.png';
								}
								// 如果还不满足，判断当前时间是否大于 signstar
								else if (currentTime > new Date(item.signStart)) {
									item.statetype = 4;
									item.urlimg = 'https://web.ptgolf.net/image/webchat/19.png';
								}
								// 如果都不满足，默认设置为 statetype 5 
								else {
									item.statetype = 5;
									item.urlimg = 'https://web.ptgolf.net/image/webchat/20.png';
								}
							});

							this.datalist = this.datalist.concat(array)
							this.pagetotal = res.data.data.total
							if (this.pagetotal == this.datalist.length || res.data.data.total == 0) {
								this.status = 'nomore'
							}

						})
			},
			// 打开选择框
			openselect() {
				this.show = true
			},
			// 点击选择框的取消
			cancel() {
				this.show = false
			},
			// 点击选择框的确定
			confirm(e) {
				if (e.indexs[0] == 0) {
					this.statename = '比赛状态 : 全部'
					this.query.raceState = ''
					this.datalist = []
					this.query.pagNum = 1
					this.eventlist()
					this.cancel()
					return
				}
				this.statename = '比赛状态 : ' + e.value[0]
				this.query.raceState = e.indexs[0] - 1
				this.datalist = []
				this.query.pagNum = 1
				this.eventlist()
				this.cancel()
			},
			// 翻页请求下一页
			lower() {
				this.status = 'loading'
				this.query.pagNum++
				this.eventlist()
			},
			// input的回车事件
			inputconfirm(e) {
				this.query.raceName = e
				this.datalist = []
				this.query.pagNum = 1
				this.eventlist()
			},
			// 点击每个赛事的详情,已结束,进行中,报名中,已报满
			itemdetail(e) {
				if (e.statetype == 1) {
					uni.navigateTo({
						url: '/subpackage1/eventpage/event_end?id=' + encodeURIComponent(e.id)
					});
				} else if (e.statetype == 2) {
					uni.navigateTo({
						url: '/subpackage1/eventpage/event_progress?id=' + encodeURIComponent(e.id)
					});
				} else if (e.statetype == 3) {
					uni.navigateTo({
						url: '/subpackage1/eventpage/event_introduce?id=' + encodeURIComponent(e.id)
					});
				} else if (e.statetype == 4) {
					uni.navigateTo({
						url: '/subpackage1/eventpage/event_introduce?id=' + encodeURIComponent(e.id)
					});
				}else if (e.statetype == 5){
					this.$refs.uToast.show({
						type: 'warning',
						message: "赛事暂未开始",
						iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/top.png',
					
					})
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.item-block {
		background-color: #1e1e1e;
		height: 100vh;

		::-webkit-scrollbar {
			width: 0rpx;
			/* 滚动条的宽度 */
		}

		.header-search {
			padding: 20rpx 40rpx;

			::v-deep .u-input {
				background-color: #f6f7f9;
				// border: 2rpx solid #dededf;
				border-radius: 36rpx;
				box-shadow: 0 0 4rpx #b9b9ba;

				/* 添加阴影 */
				.u-icon__icon {
					color: #de3600 !important;
					font-size: 50rpx !important;
					font-weight: 700 !important;

				}
			}
		}

		.bot-item {
			height: calc(100vh - 112rpx - 40rpx);
			background-color: #f4f4f4;
			border-radius: 30rpx 30rpx 0 0;
			padding: 20rpx 40rpx;

			.select_item {
				::v-deep .u-input__content__field-wrapper__field {
					color: #1e1e1e !important;
					font-weight: 600;

				}
			}

			::v-deep .u-input {
				border-bottom: 2rpx solid #d1d1d1;

			}

			.item-list {
				margin-top: 20rpx;

				.polular_list_item {
					background-size: 100% auto;
					margin-bottom: 20rpx;
					height: 160rpx;
					background-color: #fff;
					border-radius: 20rpx;
					box-shadow: 0 0 4rpx #b9b9ba;
					padding: 16rpx 30rpx;
					display: flex;
					justify-content: space-between;
					position: relative;

					.but_img {
						position: absolute;
						height: 50rpx;
						width: 70rpx;
						background-size: 100% 100%;
						bottom: 26rpx;
						right: 144rpx;
						z-index: 9;
					}

					.item_left {
						display: flex;
						flex-direction: column;
						justify-content: space-between;

						.title_item {
							font-size: 32rpx;
							font-weight: 600;
						}

						.team_item {
							font-size: 24rpx;
							color: #afafaf;
						}

						.address_item {
							margin-top: 40rpx;

							.icon-dizhi {
								font-size: 26rpx;
								border-radius: 20rpx;
								padding: 4rpx;
								background-color: #de3600;
								color: #fff;
								margin-right: 6rpx;
							}

							.address_text {
								font-size: 24rpx;
								color: #afafaf;
							}
						}
					}

					.item_right {
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						align-items: flex-end;

						.right_top {
							font-size: 20rpx;
							color: #afafaf;
							display: flex;
							flex-direction: column;
							align-items: flex-end;

						}

						::v-deep .u-button {
							color: #f6f7f9;
							border: 0rpx;
							border-radius: 60rpx;
							background-color: #de3600;
							height: 55rpx;
							width: 140rpx;
						}
					}
				}

			}
		}
	}
</style>